home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
54655
/
54655.xpi
/
chrome
/
gathererPopup.jar
/
content
/
base.js
next >
Wrap
Text File
|
2009-12-15
|
7KB
|
243 lines
Function.prototype.bind = function() {
var method = this;
var args = Array.prototype.slice.call(arguments);
var obj = args.shift();
return function BOUND_METHOD() {
return method.apply(obj,
args.concat(Array.prototype.slice.call(arguments)));
};
}
var gathererPopup = {
///////////////////////////////////////////////////////////////////
// General Utilities
///////////////////////////////////////////////////////////////////
rand: function rand(max) {
return Math.floor(Math.random() * max);
},
safeObj: function safeObj(obj) {
return obj ? obj : {};
},
safeCall: function safeCall(func) {
return func ? func : function() {};
},
hashKeysToArray: function hashKeysToArray(hash) {
var ret = [];
for (var i in hash)
ret.push(i);
return ret;
},
hashToArray: function hashToArray(hash) {
var ret = [];
for each (var i in hash)
ret.push(i);
return ret;
},
arrayToHash: function arrayToHash(array, keyfunc) {
var ret = {};
for (var i=0; i<array.length; ++i)
ret[keyfunc(array[i])] = array[i];
return ret;
},
escapeHTML: function escapeHTML(str) {
return str.replace(/&/g, "&").
replace(/</g, "<").
replace(/>/g, ">").
replace(/"/g, """);
},
_unescapeDiv: null,
unescapeHTML: function(str) {
try {
if (!this._unescapeDiv)
this._unescapeDiv = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
this._unescapeDiv.innerHTML = this.removeHTML(str);
return this._unescapeDiv.textContent;
} catch (e) {
return this.removeHTML(str);
}
},
removeHTML: function(str) {
return str.replace(/<.*?>/g, "");
},
getRootWindow: function getRootWindow() {
return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIWebNavigation).
QueryInterface(Components.interfaces.nsIDocShellTreeItem).
rootTreeItem.
QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindow);
},
openUrl: function (url){
Application.activeWindow.open(this.makeUri(url)).focus();
},
isActiveWindow: function() {
return this.windowMediator.getMostRecentWindow("navigator:browser") == window;
},
///////////////////////////////////////////////////////////////////
// Date formatting
///////////////////////////////////////////////////////////////////
getTime: function getTime() {
return (new Date()).getTime();
},
getUnixTime: function getUnixTime() {
return Math.floor((new Date()).getTime() / 1000);
},
///////////////////////////////////////////////////////////////////
// nsIChannel related functions
///////////////////////////////////////////////////////////////////
makeUri: function makeUri(link, base) { // base is optional
try {
return this.iioService.newURI(link, null, base);
} catch (e) {
return null;
}
},
get: function get(url, callback) {
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if(req.status == 200)
callback(req.responseText);
}
};
req.send(null);
return req;
},
///////////////////////////////////////////////////////////////////
// XPCOM Services
///////////////////////////////////////////////////////////////////
_iioService: null,
get iioService() {
if (!this._iioService)
this._iioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
return this._iioService;
},
_promptService: null,
get promptService() {
if (!this._promptService)
this._promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
return this._promptService;
},
_runtimeService: null,
get runtimeService() {
if (!this._runtimeService)
this._runtimeService = Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime);
return this._runtimeService;
},
_windowMediator: null,
get windowMediator() {
if (!this._windowMediator)
this._windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
return this._windowMediator;
},
///////////////////////////////////////////////////////////////////
// Debugging
///////////////////////////////////////////////////////////////////
dumpFormat: function dumpFormat(obj) {
var str;
switch (typeof obj) {
case "function":
str = "function()";
break;
default:
str = obj + "";
break;
}
if (str.length > 200)
str = str.substr(0, 200) + "............";
return str;
},
dump2: function dump2(x) {
dump(x);
return x;
},
dumps: function dumps(obj) {
var ret = "";
ret += this.dump2("**** " + this.dumpFormat(obj) + " ****\n");
for (var i in obj) {
ret += this.dump2("." + i + " = ");
try {
ret += this.dump2(this.dumpFormat(obj[i]))
} catch(e) { }
ret += this.dump2("\n");
}
ret += this.dump2("\n");
return ret;
},
say: function say() {
var args = Array.prototype.slice.call(arguments); // just for cloning args
dump(args.join(", ") + "\n");
},
sayl: function sayl() {
var args = Array.prototype.slice.call(arguments); // just for cloning args
dump(args.join(", ").substr(0, 85) + "\n");
},
log: function log(msg) {
Application.console.log(msg);
this.say(msg);
},
// in normal cases, just call printStackTrace() is ok
printStackTrace: function printStackTrace(stack, skipLevels) {
var stackFrame = Components.stack.caller;
if (arguments.length > 0 && stack)
stackFrame = stack;
if (arguments.length > 1 && skipLevels > 0) {
while (skipLevels--) {
stackFrame = stackFrame.caller;
}
}
while (stackFrame) {
dump(stackFrame);
dump("\n");
stackFrame = stackFrame.caller;
}
},
assert: function assert(truth) {
if (!truth) {
this.printStackTrace(null, 1);
throw "Assertion failed";
}
}
}